home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / stdwin / Appls / dpv / dpvrestart.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-06  |  4.1 KB  |  214 lines  |  [TEXT/????]

  1. /* dpv -- ditroff previewer.  Functions to restart anywhere in the file. */
  2.  
  3. #include "dpv.h"
  4. #include "dpvmachine.h"
  5. #include "dpvoutput.h"
  6.  
  7. typedef struct _restartinfo {
  8.     long filepos;        /* File position for fseek */
  9.     int pageno;        /* External page number */
  10.     fontinfo fonts;        /* Mounted font table */
  11.     int font, size;        /* Current font and size */
  12. } restartinfo;
  13.  
  14. int        npages;        /* Number of pages so far */
  15. bool        nomore;        /* Set when the last page has been seen */
  16. restartinfo    *pagelist;    /* State needed to start each page */
  17. int        ipage;        /* Internal page in file */
  18. int        showpage;    /* Internal page displayed in window */
  19. int        prevpage;    /* Last page visited (for '-' command) */
  20. int        stoppage;    /* Internal page where to stop */
  21. FILE        *ifile;        /* The input file */
  22.  
  23.         /* In order to avoid passing the file pointer around from
  24.            parse() via t_page() to nextpage(), the input file is
  25.            made available as a global variable. */
  26.  
  27. /* Initialize everything in the right order.  Tricky! */
  28.  
  29. initialize(filename, firstpage)
  30.     char *filename;
  31.     int firstpage;
  32. {
  33.     ifile= fopen(filename, "r");
  34.     if (ifile == NULL)
  35.         error(ABORT, "%s: cannot open", filename);
  36.     setpageinfo(ftell(ifile), 0); /* Page 0 (== header) starts here */
  37.     
  38.     showpage= -1; /* Show no pages */
  39.     stoppage= 1; /* Stop at beginning of page 1 */
  40.     parse(ifile); /* Read the header */
  41.     
  42.     initoutput(filename); /* Create the window */
  43.     
  44.     showpage= 1;
  45.     skiptopage(firstpage);
  46. }
  47.  
  48. /* Close the file */
  49.  
  50. cleanup()
  51. {
  52.     fclose(ifile);
  53. }
  54.  
  55. /* Skip n pages forward, default 1 */
  56.  
  57. forwpage(n)
  58.     int n;
  59. {
  60.     if (n <= 0)
  61.         n= 1;
  62.     gotopage(showpage+n);
  63. }
  64.  
  65. /* Skip n pages back, default 1 */
  66.  
  67. backpage(n)
  68.     int n;
  69. {
  70.     if (n <= 0)
  71.         n= 1;
  72.     gotopage(showpage-n);
  73. }
  74.  
  75. /* Go to internal page number n, and force a redraw */
  76.  
  77. gotopage(n)
  78.     int n;
  79. {
  80.     int saveshowpage= showpage;
  81.     skiptopage(n);
  82.     if (showpage != saveshowpage)
  83.         prevpage= saveshowpage;
  84.     changeall();
  85. }
  86.  
  87. /* Skip to internal page number n -- don't force a redraw */
  88.  
  89. static
  90. skiptopage(n)
  91.     int n;
  92. {
  93.     int orign= n;
  94.     if (n <= 0)
  95.         n= 1;
  96.     if (n == showpage) {
  97.         if (orign < n)
  98.             wfleep();
  99.         return;
  100.     }
  101.     if (n >= npages) {
  102.         if (nomore) {
  103.             n= npages-1;
  104.             if (n == showpage) {
  105.                 wfleep();
  106.                 return;
  107.             }
  108.             showpage= n;
  109.         }
  110.         else {
  111.             backtopage(npages-1);
  112.             showpage= -1;
  113.             stoppage= n;
  114.             parse(ifile);
  115.             showpage= npages-1;
  116.         }
  117.     }
  118.     else
  119.         showpage= n;
  120. }
  121.  
  122. /* Draw procedure */
  123.  
  124. void
  125. drawproc(drawwin, left, top, right, bottom)
  126.     WINDOW *drawwin;
  127.     int left, top, right, bottom;
  128. {
  129.     topdraw= top;
  130.     botdraw= bottom;
  131.     backtopage(showpage);
  132.     stoppage= showpage+1;
  133.     parse(ifile);
  134. }
  135.  
  136. /* Record the current file position as the start of the page
  137.    with (external) page number 'pageno'.
  138.    Note that the 'p' command has already been consumed by the parser.
  139.    Return < 0 if the parser can stop parsing. */
  140.  
  141. int
  142. nextpage(pageno)
  143.     int pageno;
  144. {
  145.     ++ipage;
  146.     setpageinfo(ftell(ifile), pageno);
  147.     if (ipage >= stoppage)
  148.         return -1; /* Stop parsing */
  149.     else
  150.         return 0;
  151. }
  152.  
  153. /* Indicate that the end of the input has been reached.
  154.    No more new pages will be accepted. */
  155.  
  156. lastpage()
  157. {
  158.     nomore= TRUE;
  159. }
  160.  
  161. /* Store info about coming page.
  162.    Called at start of file and after 'p' command processed */
  163.  
  164. static
  165. setpageinfo(filepos, pageno)
  166.     long filepos;
  167.     int pageno;
  168. {
  169.     if (statep != state)
  170.         error(FATAL, "setpageinfo: {} stack not empty");
  171.     if (ipage < npages) {
  172.         /* We've been here already.
  173.            Might as well check consistency. */
  174.         /* HIRO */
  175.     }
  176.     else if (ipage > npages)
  177.         error(ABORT, "setpageinfo: ipage>npages (can't happen)");
  178.     else {
  179.         restartinfo r;
  180.         r.filepos= filepos;
  181.         r.pageno= pageno;
  182.         r.fonts= fonts;
  183.         r.font= font;
  184.         r.size= size;
  185.         L_APPEND(npages, pagelist, restartinfo, r);
  186.         if (pagelist == NULL)
  187.             error(FATAL, "out of mem for pagelist");
  188.     }
  189. }
  190.  
  191. /* Position the input stream at the start of internal page i
  192.    and restore the machine state to the state remembered for that page */
  193.  
  194. static
  195. backtopage(i)
  196.     int i;
  197. {
  198.     restartinfo *p;
  199.     
  200.     if (i < 0 || i >= npages)
  201.         error(ABORT, "backtopage: called with wrong arg");
  202.     p= &pagelist[i];
  203.     if (fseek(ifile, p->filepos, 0) < 0)
  204.         error(FATAL, "backtopage: can't fseek");
  205.     ipage= i;
  206.     fonts= p->fonts;
  207.     font= p->font;
  208.     size= p->size;
  209.     hpos= 0;
  210.     vpos= 0;
  211.     statep= state;
  212.     usefont();
  213. }
  214.